<!--
 * @Description: 基础调价维护
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 14:25:23
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="tabtop">
          <div v-for="(item, index) in contractTypelist" :key="index" @click="choose(item.id)">
            <div :class="currenindex == index + 1 ? 'active' : 'text'">{{ item.value }}</div>
            <div v-if="currenindex !== index + 1 && currenindex !== index + 2 && index !== 2" class="hr"></div>
          </div>
        </div>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div>
              <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </div>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">地区</span>
                <el-select v-model="tableParam.regionId" style="width: 240px;" placeholder="请选择">
                  <el-option v-for="(item, index) in regionIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <span v-if="currenindex == 1" style="margin-left: 24px;">
                <span style="margin-right: 12px;">油费类型</span>
                <el-select v-model="tableParam.typeId" style="width: 240px;" placeholder="请选择">
                  <el-option v-for="(item, index) in oiltypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <span v-if="currenindex == 3" style="margin-left: 24px;">
                <span style="margin-right: 12px;">电费类型</span>
                <el-select v-model="tableParam.typeId" style="width: 240px;" placeholder="请选择">
                  <el-option v-for="(item, index) in electricitytypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <span v-if="currenindex == 2" style="margin-left: 24px;">
                <span style="margin-right: 12px;">水费类型</span>
                <el-select v-model="tableParam.typeId" style="width: 240px;" placeholder="请选择">
                  <el-option v-for="(item, index) in watertypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="addFromDate('添加')">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="currenindex == 1" size="small" class="bottom_all_two" style="margin-left: 12px;" @click="toURL(URls.oilPriceSite)">油价查看</el-button>
              <el-button v-if="currenindex == 1" size="small" class="bottom_three" @click="saveURl('oilPriceSite')">油价查看设置</el-button>
              <el-button v-if="currenindex == 2" size="small" class="bottom_all_two" style="margin-left: 12px;" @click="toURL(URls.waterRateSite)">水价查看</el-button>
              <el-button v-if="currenindex == 2" size="small" class="bottom_three" @click="saveURl('waterRateSite')">水价查看设置</el-button>
              <el-button v-if="currenindex == 3" size="small" class="bottom_all_two" style="margin-left: 12px;" @click="toURL(URls.energyChargeSite)">电价查看</el-button>
              <el-button v-if="currenindex == 3" size="small" class="bottom_three" @click="saveURl('energyChargeSite')">电价查看设置</el-button>
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <template v-for="(item, index) in table">
                    <el-dropdown-item v-if="!item.type" :key="index">
                      <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                    </el-dropdown-item>
                    <el-dropdown-item v-if="item.type" :key="index">
                      <el-checkbox v-model="item.status">{{ currenindex == 1 ? '油费类型' : currenindex == 2 ? '水费类型' : '电费类型' }}</el-checkbox>
                    </el-dropdown-item>
                  </template>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="load()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div v-loading="tableLoading" class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark">
              <template v-for="(item, index2) in table">
                <el-table-column v-if="item.status && !item.type" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row, '修改')">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <div v-else-if="item.label == '价格'"> {{scope.row[item.prop]}}{{currenindex == 1 ? '（元/升）' : currenindex == 2 ? '（元/方）' : '（元/度）'}}</div>
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
                <el-table-column v-if="item.status && item.type" :key="index2" :label="currenindex == 1 ? '油费类型' : currenindex == 2 ? '水费类型' : '电费类型'" align="center" :width="item.width">
                  <template slot-scope="scope">
                    <span>{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="580px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="companyId">
            <span slot="label" style="color:  #FF0000 ;">公司</span>
            <el-select ref="selection1" v-model="fromDate.companyId" placeholder="请选择" @change="$nextTick(()=>{fromDate.company = $refs.selection1.selectedLabel})">
              <el-option v-for="item in companylist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="regionId">
            <span slot="label" style="color:  #FF0000 ;">地区</span>
            <el-select v-model="fromDate.regionId" placeholder="请选择">
              <el-option v-for="(item, index) in regionIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="currenindex == 1" prop="typeId">
            <span slot="label" style="color:  #FF0000 ;">油费类型</span>
            <el-select v-model="fromDate.typeId" placeholder="请选择">
              <el-option v-for="(item, index) in oiltypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="currenindex == 2" prop="typeId">
            <span slot="label" style="color:  #FF0000 ;">水费类型</span>
            <el-select v-model="fromDate.typeId" placeholder="请选择">
              <el-option v-for="(item, index) in watertypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="currenindex == 3" prop="typeId">
            <span slot="label" style="color:  #FF0000 ;">电费类型</span>
            <el-select v-model="fromDate.typeId" placeholder="请选择">
              <el-option v-for="(item, index) in electricitytypeIdList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="money">
            <span slot="label" style="color:  #FF0000 ;">价格
            </span>
            <el-input v-model="fromDate.money" placeholder="请输入" onkeypress='return( /\d|\./.test(String.fromCharCode(event.keyCode)))' @input="fromDate.money=$regExp.checkNum(fromDate.money)" @blur="fromDate.money=$regExp.blurCheckNum(fromDate.money)">
              <template slot="append"> {{currenindex == 1 ? '（元/升）' : currenindex == 2 ? '（元/方）' : '（元/度）'}}</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="effectiveDate">
            <span slot="label" style="color:  #FF0000 ;">生效日期</span>
            <el-date-picker v-model="fromDate.effectiveDate" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  saveOrUpdate,
  Download,
  getPricingSite,
  savePricingSite,
} from "@/api/basicConfig/abnormal.js";
import { getDicType, permissionsCompanyList } from "@/api/common.js";
export default {
  mixins: [mixin],
  data() {
    return {
      queryPage: queryPage,
      //batchUpdateOrDelete:batchUpdateOrDelete,
      // updateSortNum:updateSortNum,
      saveOrUpdate: saveOrUpdate,
      DownloadURL: Download,
      /*表单页类型，0为无树状，1为有树状，有树状在el-main加上display:flex*/
      pagesType: 0,
      /*添加表单数据*/
      contractTypelist: [
        {
          value: "油费调价",
          id: 1,
        },
        {
          value: "水费调价",
          id: 2,
        },
        {
          value: "电费调价",
          id: 3,
        },
      ],
      currenindex: 1,
      table: [
        { label: "调价ID", width: "auto", prop: "code", status: true },
        { label: "地区", width: "auto", prop: "region", status: true },
        {
          label: "类型",
          width: "auto",
          prop: "type",
          status: true,
          type: true,
        },
        { label: "价格", width: "auto", prop: "money", status: true },
        {
          label: "生效时间",
          width: "auto",
          prop: "effectiveDate",
          status: true,
        },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      rules: {
        companyId: [
          { required: true, message: "请选择公司", trigger: "change" },
        ],
        money: [{ required: true, message: "请输入价格", trigger: "blur" }],
        regionId: [
          { required: true, message: "请选择地区", trigger: "change" },
        ],
        typeId: [{ required: true, message: "请选择类型", trigger: "change" }],
        effectiveDate: [
          { required: true, message: "请选择生效时间", trigger: "change" },
        ],
      },
      fromDate: {
        basicPricingType: "",
        region: "",
        regionId: "",
        type: "",
        typeId: "",
        money: null,
        effectiveDate: "", //生效时间
      },
      regionIdList: [],
      oiltypeIdList: [],
      watertypeIdList: [],
      electricitytypeIdList: [],
      URls: {},
      companylist: []
    };
  },
  async created() {
    this.companylist = await permissionsCompanyList()
    this.tableParam = {
      ...this.tableParam,
      basicPricingType: this.currenindex,
      regionId: "",
      typeId: "",
    };
    this.gettableData();
    getDicType({ code: "region" }).then((res) => {
      this.regionIdList = res.data.items;
    });
    getDicType({ code: "waterType" }).then((res) => {
      this.watertypeIdList = res.data.items;
    });
    getDicType({ code: "oilType" }).then((res) => {
      this.oiltypeIdList = res.data.items;
    });
    getDicType({ code: "electType" }).then((res) => {
      this.electricitytypeIdList = res.data.items;
    });
    this.getURl();
  },
  watch: {
    currenindex: {
      //data所有属性都进行监听器，可使用字符串的形式监听对象属性（'item.item1'）
      handler() {
        this.tableParam.basicPricingType = this.currenindex;
        this.tableParam.page = 1;
        this.init();
      },
      deep: false,
      immediate: false,
    },
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    init() {
      /*重置数据*/
      this.tableParam.regionId = "";
      this.tableParam.typeId = "";
      this.gettableData();
    },
    choose(num) {
      this.currenindex = num;
    },
    getURl() {
      getPricingSite().then((res) => {
        this.URls = res.data;
      });
    },
    toURL(url) {
      if (!url) {
        this.$message.warning('请设置后在进行查看！！！');
        return
      }
      window.open(url);
    },
    saveURl(a) {
      this.$prompt(`原地址：${this.URls[a] ? this.URls[a] : '未配置'}`, "输入链接地址", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "bottom_all_two",
        confirmButtonClass: "bottom_all",
        inputPlaceholder: "输入完整URL，加上https://或http://",
        inputPattern: /(ht|f)tp(s?):\/\/[0-9a-zA-Z]?/,
        inputErrorMessage: "请输入完整的链接地址",
      })
        .then(({ value }) => {
          this.URls[a] = value;
          savePricingSite(this.URls).then((res) => {
            if (res.code * 1 === 1001) {
              this.$message.success('设置成功！！！')
            }
          });
        })
        .catch(() => { });
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          this.fromDate.basicPricingType = this.currenindex;
          let list = _this.oiltypeIdList;
          if (_this.currenindex == 2) {
            list = _this.watertypeIdList;
          }
          if (_this.currenindex == 3) {
            list = _this.electricitytypeIdList;
          }
          _this.fromDate.region = _this.regionIdList.find(function (obj) {
            return obj.id == _this.fromDate.regionId;
          }).itemName;
          _this.fromDate.type = list.find(function (obj) {
            return obj.id == _this.fromDate.typeId;
          }).itemName;
          _this.submitFromDate();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.gettableData();
    },
  },
};
</script>
<style lang="scss">
.el-date-editor--datetimerange.el-input__inner {
  width: 320px;
}
</style>
<style lang="scss" scoped>
.el-input,
.el-select {
  width: 320px;
}

.dialoginfo {
  padding: 0 20px 0 20px;
}
.dialoginfo-title {
  display: flex;
  align-items: center;
  height: 50px;
  > div {
    width: 4px;
    height: 20px;
    background: $bg_color;
  }
  > span {
    padding-left: 10px;
    font-size: 20px;
    color: $bg_color;
    font-weight: bold;
  }
}
.tabtop {
  width: 571px;
}
</style>
